package com.jrh.web.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jrh.biz.brand.BrandService;
import com.jrh.biz.img.ImgUploadService;
import com.jrh.dal.ImgAdDao;
import com.jrh.domain.entity.Brand;
import com.jrh.domain.entity.ImgAd;
import com.jrh.domain.user.LoginUserInfo;
import com.jrh.web.member.tools.UserAuthUtil;

/**
 * 商家后台品牌相关控制器
 * 
 * @author yout 2012-7-22 上午11:16:12
 */
@Controller
@RequestMapping("/seller")
public class SellerBrandController {

    private static final String UPLOAD_TYPE = "brand";

    @Autowired
    private BrandService        brandService;

    @Autowired
    private ImgUploadService    imgUploadService;

    @Autowired
    private ImgAdDao            imgAdDao;

    // 设置
    @RequestMapping(value = "/brand_config", method = RequestMethod.GET)
    public String brandConfig(Model model, HttpServletRequest request, HttpServletResponse response, String success) {
        LoginUserInfo userInfo = UserAuthUtil.getUserInfo(request, response);
        Brand brand = brandService.getBrandByUserId(userInfo.getUserId());
        model.addAttribute("brand", brand);
        model.addAttribute("success", Boolean.valueOf(success));
        return "admin/brand/config";
    }

    @RequestMapping(value = "/brand_config", method = RequestMethod.POST)
    public String brandConfig(Brand brand, Model model, HttpServletRequest request, HttpServletResponse response) {
        LoginUserInfo userInfo = UserAuthUtil.getUserInfo(request, response);
        Brand oldBrand = brandService.getBrandByUserId(userInfo.getUserId());
        brand.setId(oldBrand.getId());
        brandService.modifyBrand(brand);
        return "redirect:/seller/brand_config?success=" + Boolean.TRUE.toString();
    }

    // 广告
    @RequestMapping(value = "/brand_ad", method = RequestMethod.GET)
    public String brandAd(Model model, HttpServletRequest request, HttpServletResponse response, String success) {
        LoginUserInfo userInfo = UserAuthUtil.getUserInfo(request, response);
        Brand brand = brandService.getBrandByUserId(userInfo.getUserId());
        ImgAd imgAd = new ImgAd(brand.getId(), ImgAd.AD_TYPE_BRAND);
        List<ImgAd> list = imgAdDao.listByCondition(imgAd);
        model.addAttribute("list", list);
        return "admin/brand/adimg";
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/brand/brand_ad/delete/{id}")
    public String deleteImgAd(@PathVariable("id") long id) {
        try {
            imgAdDao.delete(id);
            return Boolean.TRUE.toString();
        } catch (Exception e) {
            return Boolean.FALSE.toString();
        }
    }

    @RequestMapping("/brand_ad/add")
    public String addImgAd(ImgAd imgAd, MultipartFile imgFile, HttpServletRequest request, HttpServletResponse response) {
        LoginUserInfo userInfo = UserAuthUtil.getUserInfo(request, response);
        Brand brand = brandService.getBrandByUserId(userInfo.getUserId());
        imgAd.setRefId(brand.getId());
        imgAd.setRefType(ImgAd.AD_TYPE_BRAND);
        String imgPath = imgUploadService.upload(imgFile, UPLOAD_TYPE);
        imgAd.setImgPath(imgPath);
        imgAdDao.create(imgAd);
        return "redirect:/seller/brand_ad";
    }
}
